package com.dianjing.dianqun.api.dianpai;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dianjing.dianqun.common.ResponseData;
import com.dianjing.dianqun.common.base.BaseRestController;
import com.dianjing.dianqun.model.DianPaiCommentReply;
import com.dianjing.dianqun.model.DianPaiContent;
import com.dianjing.dianqun.model.DianPaiContentComment;
import com.dianjing.dianqun.model.Site;
import com.dianjing.dianqun.model.User;
import com.dianjing.dianqun.service.SiteService;
import com.dianjing.dianqun.service.UserService;
import com.dianjing.dianqun.service.dianpai.DianPaiCommentReplyService;
import com.dianjing.dianqun.service.dianpai.DianPaiContentCommentService;
import com.dianjing.dianqun.service.dianpai.DianPaiContentService;
import com.dianjing.dianqun.utils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @date : 2017年10月26日
 * 
 * @author: baoyongtao
 * 
 * @see :
 * 
 */
@Api(description = "点拍评论相关api", value = "DianPaiCommentApiController")
@RestController
@RequestMapping("/api/comment")
public class DianPaiCommentApiController extends BaseRestController {

	@Autowired
	private UserService userService;

	@Autowired
	private DianPaiContentService contentService;

	@Autowired
	private DianPaiContentCommentService commentService;

	@Autowired
	private SiteService siteService;

	@Autowired
	private DianPaiCommentReplyService commentReplyService;

	@ApiImplicitParam(dataType = "int", name = "id", required = true, value = "评论id", paramType = "path")
	@ApiOperation(value = "根据评论id获取评论详情", notes = "根据评论id获取评论详情")
	@GetMapping("/{id}")
	public ResponseData get(@PathVariable(required = true) Integer id,
			@RequestParam(required = true, value = "userId") Integer userId) {
		DianPaiContentComment contentComment = commentService.selectByPrimaryKey(id);
		if (null == contentComment) {
			return new ResponseData(ResponseData.STATUS_ERROR, "未查询有该评论", id);
		}
		List<DianPaiCommentReply> list = commentReplyService.findReplyByCommetId(id, userId);
		contentComment.setReplyList(list);
		return new ResponseData(ResponseData.STATUS_OK, "查询完成", contentComment);
	}

	@ApiImplicitParams({
			@ApiImplicitParam(dataType = "int", name = "contentId", required = true, value = "点拍文章id", paramType = "path"),
			@ApiImplicitParam(dataType = "int", name = "commentId", required = false, value = "评论id", paramType = "query"),
			@ApiImplicitParam(dataType = "int", name = "toUserId", required = false, value = "被评论人id", paramType = "query"),
			@ApiImplicitParam(dataType = "int", name = "fromUserId", required = true, value = "评论人id", paramType = "query"),
			@ApiImplicitParam(dataType = "int", name = "comment", required = true, value = "评论内容", paramType = "query") })
	@PostMapping("/save/{contentId}")
	@ApiOperation(value = "保存评论", notes = "保存评论，后台审核")
	public ResponseData save(@PathVariable(required = true) Integer contentId,
			@RequestParam(required = false) Integer commentId, @RequestParam(required = false) Integer toUserId,
			@RequestParam(required = true) Integer fromUserId, @RequestParam(required = true) String comment) {
		if (StringUtils.isEmpty(comment)) {
			return new ResponseData(ResponseData.STATUS_ERROR, "评论不能为空", contentId);
		}
		if (null == commentId) {// 新评论 大评论 文章id不为空
			DianPaiContent paiContent = contentService.selectByPrimaryKey(contentId);
			if (null == paiContent) {// 文章不存在
				return new ResponseData(ResponseData.STATUS_ERROR, "文章不存在", contentId);
			}
			if (fromUserId != null && toUserId == null) {
				User fromUser = userService.selectByPrimaryKey(fromUserId);
				if (fromUser == null) {
					return new ResponseData(ResponseData.STATUS_ERROR, "用户不存在", fromUser);
				}
				// 开始保大评论
				DianPaiContentComment contentComment = new DianPaiContentComment();
				contentComment.setCommentUserId(fromUserId);// 评论者
				contentComment.setCreateTime(new Date());
				contentComment.setAuthorUserId(paiContent.getUserId());// 作者
				contentComment.setContentId(contentId);// 文章id
				contentComment.setIsChecked(-1);// 审核状态
				contentComment.setComment(comment);
				Site site = siteService.selectSitelByUserId(paiContent.getUserId());
				contentComment.setSiteId(site.getSiteId());
				try {
					commentService.insertSelective(contentComment);// 保存 }
				} catch (Exception e) {
					return new ResponseData(ResponseData.STATUS_ERROR, "保存失败，可能用户不存在", fromUserId);
				}
				return new ResponseData(ResponseData.STATUS_OK, "保存完成", contentComment.getCommentId());
			}
		}
		// 小评论
		if (fromUserId != null && toUserId != null && commentId != null) {
			DianPaiContentComment contentComment = commentService.selectByPrimaryKey(commentId);
			if (null == contentComment) {
				return new ResponseData(ResponseData.STATUS_ERROR, "没有此条评论", contentId);
			}

			DianPaiCommentReply commentReply = new DianPaiCommentReply();
			commentReply.setComment(comment);
			commentReply.setCommentId(commentId);
			commentReply.setInsertDate(new Date());
			commentReply.setToUserId(toUserId);
			commentReply.setFromUserId(fromUserId);
			commentReply.setIsChecked(-1);
			commentReplyService.insertSelective(commentReply);
			return new ResponseData(ResponseData.STATUS_OK, "查询完成", commentReply.getReplyId());
		}
		return new ResponseData(ResponseData.STATUS_OK, "查询完成", "");
	}

	@ApiOperation(value = "查看回复对话", notes = "根据所属评论id查看回复对话")
	@ApiImplicitParam(dataType = "int", name = "commentId", required = true, value = "大评论id", paramType = "path")
	@GetMapping("/reply/{commentId}")
	public ResponseData queryComment(@PathVariable(required = true) Integer commentId,
			@RequestParam(value = "userId", required = true) Integer userId) {
		return new ResponseData(ResponseData.STATUS_OK, "查询完成",
				commentReplyService.findReplyByCommetId(commentId, userId));
	}

	@GetMapping("/query/content/{contentId}")
	@ApiOperation(value = "根据点拍文章id分页查询评论", notes = "分页查询评论")
	@ApiImplicitParams({
			@ApiImplicitParam(dataType = "int", name = "contentId", required = true, value = "点拍文章id", paramType = "path"),
			@ApiImplicitParam(dataType = "int", name = "userId", value = "当前用户id", paramType = "query"),
			@ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页多少记录", paramType = "query"),
			@ApiImplicitParam(dataType = "int", name = "pageNum", value = "当前页", paramType = "query") })
	public ResponseData get(@PathVariable(required = true) Integer contentId,
			@RequestParam(value = "userId" , required = false) Integer userId, @RequestParam(defaultValue = "10") Integer pageSize,
			@RequestParam(defaultValue = "1") Integer pageNum) {
		DianPaiContent content = contentService.selectByPrimaryKey(contentId);
		if (content == null) {
			return new ResponseData(ResponseData.STATUS_ERROR, "文章不存在", contentId);
		}
		PageHelper.startPage(pageNum, pageSize);
		List<DianPaiContentComment> list = commentService.selectByContentId(contentId, userId);
		PageInfo<DianPaiContentComment> pageInfo = new PageInfo<DianPaiContentComment>(list);
		return new ResponseData(ResponseData.STATUS_OK, "查询完成", pageInfo);
	}
}
